package com.ruoyi.hxf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hxf.domain.Pd;
import com.ruoyi.hxf.domain.Type;
import com.ruoyi.hxf.domain.bo.PayBo;
import com.ruoyi.hxf.domain.vo.PayVo;
import com.ruoyi.hxf.mapper.RosterMapper;
import com.ruoyi.hxf.service.IPayService;
import com.ruoyi.hxf.service.IPdService;
import com.ruoyi.system.domain.vo.SysOssVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 缴费信息
 *
 * @author hxf
 * @date 2023-01-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/money/pd")
public class pdController extends BaseController {
    private final IPdService iPdService;
    private final RosterMapper rosterMapper;

    /**
     * 查询我的缴费信息列表(电脑端)
     */
    @GetMapping("/list/{userName}/{nikeName}")
    public TableDataInfo<Pd> list(Pd pd, PageQuery pageQuery, @PathVariable("userName") String userName, @PathVariable("nikeName") String nikeName) {
        TableDataInfo<Pd> payVoTableDataInfo = iPdService.queryPageList(pd, pageQuery, userName, nikeName);
        for (Pd row : payVoTableDataInfo.getRows()) {
            if (StringUtils.isNotBlank(rosterMapper.selectClassName(Long.valueOf(row.getApp().getClassId())))) {
                row.setRemark(rosterMapper.selectClassName(Long.valueOf(row.getApp().getClassId())));
            } else {
                row.setRemark("家长异常操作");
            }
        }
        return payVoTableDataInfo;
    }


    /**
     * 获取缴费信息（手机端）
     *
     * @param pdId 主键
     */
    @GetMapping("/{pdId}")
    public R<Pd> getInfo(@NotNull(message = "主键不能为空")
                         @PathVariable Long pdId) {
        Long userId = LoginHelper.getUserId();

        return R.ok(iPdService.queryById(userId, pdId));
    }

    /**
     * 缴费（修改缴费状态）
     */
    @Log(title = "缴费（修改缴费状态）", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/{pdId}")
    public R<Void> edit(@NotNull(message = "主键不能为空")
                        @PathVariable Long pdId) {
        Long userId = LoginHelper.getUserId();
        Pd pd = iPdService.queryById(userId, pdId);
        pd.setCreateTime(DateUtils.getNowDate());
        return toAjax(iPdService.MyUpdateByBo(pd));
    }

    /**
     * 获取缴费记录信息
     */
    @GetMapping("")
    public TableDataInfo<Pd> getRecord(Pd pd, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        pd.setCreateBy(String.valueOf(userId));
        return iPdService.queryPagePdList(pd, pageQuery);
    }

    /**
     * 删除缴费记录
     *
     * @param pdIds 主键串
     */
    @Log(title = "删除缴费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pdIds}")
    public R<Void> deleteMyPd(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] pdIds) {
        return toAjax(iPdService.deleteWithValidByIds(Arrays.asList(pdIds), true));
    }

    /**
     * 缴费记录信息列表(电脑端)
     */
    @GetMapping("/list/{userName}/{nikeName}/{payName}")
    public TableDataInfo<Pd> recordList(Pd pd, PageQuery pageQuery, @PathVariable("userName") String userName, @PathVariable("nikeName") String nikeName, @PathVariable("payName") String payName) {
        TableDataInfo<Pd> payVoTableDataInfo = iPdService.queryPageRecordList(pd, pageQuery, userName, nikeName,payName);
        for (Pd row : payVoTableDataInfo.getRows()) {
            if (StringUtils.isNotBlank(rosterMapper.selectClassName(Long.valueOf(row.getApp().getClassId())))) {
                row.setRemark(rosterMapper.selectClassName(Long.valueOf(row.getApp().getClassId())));
            } else {
                row.setRemark("家长异常操作");
            }
        }
        return payVoTableDataInfo;
    }
    /**
     * 删除缴费记录(电脑端)
     *
     * @param pdIds 主键串
     */
    @Log(title = "删除缴费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/record/{pdIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                              @PathVariable Long[] pdIds) {
        return toAjax(iPdService.deleteRecordWithValidByIds(Arrays.asList(pdIds), true));
    }

}
